package com.huilam.restactions.services.actions;

import java.text.SimpleDateFormat;
import java.util.Date;
import com.huilam.restactions.common.RESTAction;
import com.huilam.restactions.common.HttpReqClient;
import com.huilam.restactions.interfaces.IRESTAction;

public class ServerTime implements IRESTAction {

	@Override
	public String doActions(RESTAction restActionConfig, HttpReqClient reqClient) {
		
		/*
		 * [restaction.property]
		 * 
		 * time.path=/time/{format}
		 * time.classname=com.huilam.restactions.services.actions.ServerTime
		 * 
		 */

		String sDateTime = null;
		//
		String sDateFormat = reqClient.getRESTUrlParam("format");
		if(sDateFormat!=null)
		{
			try{
				SimpleDateFormat sf = new SimpleDateFormat(sDateFormat);
				sDateTime = sf.format(new Date());
			}
			catch(IllegalArgumentException ex)
			{
				//ignore
			}
		}
		
		if(sDateTime==null)
		{
			sDateTime = new Date().toString();
		}
		return sDateTime;
	}

}
